package korwin;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public abstract class Enemy {
    // For creating new enemies.
    public static final long timeBetweenNewEnemiesInit = Framework.secInNanosec*2;
    public static long timeBetweenNewEnemies = timeBetweenNewEnemiesInit;
    public static long timeOfLastCreatedEnemy = 0;
    
    // Health of the enemy.
    public int health;
    
    // Position of the enemy on the screen.
    public double xCoordinate;
    public double yCoordinate;
    
    // Moving speed and direction.
    double movingXspeed = 0;
    double movingYspeed = 0;
    
    // Images of enemy
    public BufferedImage bodyImg = null;
    public BufferedImage frontAnimImg = null;
    public BufferedImage rearAnimImg = null;
    
    // Animation of the enemy moving parts.
    public  Animation frontAnim = null;
    public Animation rearAnim = null;
    
    // Offset for the propeler. We add offset to the position of the position of helicopter.
    public int offsetXFrontAnim = 0;
    public int offsetYFrontAnim = 0;
    public int offsetXRearAnim = 0;
    public int offsetYRearAnim = 0;

    public void Initialize()
    {
    }
    
    
    /**
     * It sets speed and time between enemies to the initial properties.
     */
    public void restartEnemy(){
        Enemy.timeBetweenNewEnemies = timeBetweenNewEnemiesInit;
        Enemy.timeOfLastCreatedEnemy = 0;
    }
    
    
    /**
     * It increase enemy speed and decrease time between new enemies.
     */
    public void speedUp(){
        if(Enemy.timeBetweenNewEnemies > Framework.secInNanosec)
            Enemy.timeBetweenNewEnemies -= Framework.secInNanosec / 100;
        this.movingXspeed -= 0.25;
    }
    
    public void hold(){
        this.movingXspeed = 0.0;
        this.movingYspeed = 0.0;
    }
    
    
    /**
     * Checks if the enemy is out of the screen.
     */
    public boolean isOutOfScreen(int frameHeight)
    {
        if(xCoordinate < 0 - bodyImg.getWidth())
            return true;
        else 
        	if(yCoordinate > frameHeight)
        		return true;
        	else
        		return false;
    }
    
        
   /**
    * Updates position of helicopter, animations.
    */
    public void Update()
    {
        // Move enemy on x coordinate.
        xCoordinate += movingXspeed;
        yCoordinate += movingYspeed;

        // Moves animations with enemy.
        if (frontAnim != null)
        	frontAnim.changeCoordinates((int) (xCoordinate + this.offsetXFrontAnim), (int) (yCoordinate + this.offsetYFrontAnim));
        if (frontAnim != null)
        	rearAnim.changeCoordinates((int) (xCoordinate + this.offsetXRearAnim), (int) (yCoordinate + this.offsetYRearAnim));
    }

    
    /**
     * Draws enemy to the screen.
     * 
     * @param g2d Graphics2D
     */
    public void Draw(Graphics2D g2d)
    { 
        g2d.drawImage(bodyImg, (int) xCoordinate, (int) yCoordinate, null);
        if (frontAnim != null)
        	frontAnim.Draw(g2d);
        if (rearAnim != null)
        	rearAnim.Draw(g2d);
    }  
}
